#include <graphics/GLFrameBufferObject.h>

namespace ne
{
    GLFrameBufferObject::GLFrameBufferObject()
        : GLObject(),
          mUsage(FBOU_NORMAL)
    {
        glGenFramebuffers(1, &mSource);
    }

    void GLFrameBufferObject::setUsage(const FrameBufferUsage usage)
    {
        mUsage = usage;
        glBindFramebuffer(mUsage, mSource);
    }

    FrameBufferUsage GLFrameBufferObject::getUsage() const
    {
        return mUsage;
    }

    void GLFrameBufferObject::bind()
    {
        glBindFramebuffer(mUsage, mSource);
    }

    bool GLFrameBufferObject::checkStatus()
    {
        glBindFramebuffer(mUsage, mSource);
        if (glCheckFramebufferStatus(mUsage) == GL_FRAMEBUFFER_COMPLETE)
        {
            return true;
        }
        return false;
    }

    bool GLFrameBufferObject::isFrameBuffer()
    {
        if (glIsFramebuffer(mSource) == GL_TRUE)
        {
            return true;
        }
        return false;
    }

    void GLFrameBufferObject::attachRenderBufferObject(GLRenderBufferObject *rbo, const FrameBufferAttachment &attachment)
    {
        glBindFramebuffer(mUsage, mSource);
        glFramebufferRenderbuffer(mUsage, attachment, GL_RENDERBUFFER, rbo->getSource());
    }

    void GLFrameBufferObject::attachTexture(Texture &texure, const FrameBufferAttachment attachment)
    {
        glBindFramebuffer(mUsage, mSource);
        switch (texure.getType())
        {
            case TT_1D:
#if NE_RENDER_SYSTEM == NE_RENDER_SYSTEM_OPENGL
                glFramebufferTexture1D(mUsage, attachment, texure.getType(),
                                       texure.getSource(), texure.getNumMipmap());
#endif
                break;

            case TT_2D:
            case TT_CUBE_MAP:
                glFramebufferTexture2D(mUsage, attachment, texure.getType(),
                                       texure.getSource(), texure.getNumMipmap());
                break;

            case TT_3D:
#if NE_RENDER_SYSTEM == NE_RENDER_SYSTEM_OPENGL
                glFramebufferTexture3D(mUsage, attachment, texure.getType(),
                                       texure.getSource(), texure.getNumMipmap(), 0);
#endif
                break;

            default:
                break;
        }
    }

    GLFrameBufferObject::~GLFrameBufferObject()
    {
        glDeleteFramebuffers(1, &mSource);
    }
}
